<template>
  <Table
    ref="tableRef"
    :url="URL_QUERYCHANNELLIST"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
    listKey="list"
  ></Table>
</template>

<script lang="ts" setup name="platformOperatorList">
import Table from '@/components/table/index.vue';
import {
  URL_QUERYCHANNELLIST,
  frozenChannel,
  deleteChannel,
} from '@/service/apis/channel';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: 'orgName',
      label: '企业名称',
    },
    {
      prop: 'operatorName',
      label: '运营商名称',
    },
    {
      prop: 'code',
      label: '统一社会信用代码',
    },
    {
      prop: 'contactUser',
      label: '联系人',
    },
    {
      prop: 'contactNum',
      label: '联系方式',
    },
    {
      prop: 'email',
      label: '邮箱',
    },
    {
      prop: 'isFrozen',
      label: '状态',
      template: (row: any, index: number) => {
        return row.isFrozen
          ? `<div style="color:#FA3939;">冻结</div>`
          : `<div style="color:#01C68B;">正常</div>`;
      },
    },
  ],
  handles: [
    {
      label: '配置',
      permission: 'platformOperatorList.set',
      handle: (row: any) => {
        router.push({
          name: 'platformOperatorConfig',
          query: { id: row.id },
        });
      },
    },
    {
      label: '详情',
      handle: (row: any) => {
        router.push({
          name: 'platformOperatorDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '冻结',
      permission: 'platformOperatorList.frozen',
      labelFun: (row: any) => {
        return row.isFrozen ? '恢复' : '冻结';
      },
      colorFun: (row: any) => {
        return row.isFrozen ? '#01C68B' : '#FA3939';
      },
      handle: (row: any) => {
        const msg = row.isFrozen
          ? '是否确认执行本操作？'
          : '平台运营商冻结后，该运营商电站不再提供服务、用户无法登录移动端，是否确认执行本操作？';
        ElMessageBox.confirm(msg, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          frozenChannel({
            id: row.id,
            isFrozen: !row.isFrozen,
          }).then((res: any) => {
            if (res) {
              row.isFrozen = res.isFrozen;
            }
          });
        });
      },
    },
    {
      label: '修改',
      permission: 'platformOperatorList.edit',
      handle: (row: any) => {
        router.push({
          name: 'platformOperatorEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'platformOperatorList.delete',
      handle: (row: any) => {
        ElMessageBox.confirm(
          '此操作会将该运营商及所有企业一并删除，是否确定删除？',
          '提示',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(() => {
          deleteChannel({
            id: row.id,
          }).then((res: any) => {
            if (res) {
              tableRef.value.getData();
            }
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'platformOperatorList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'platformOperatorAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '企业名称',
      prop: 'orgName',
      comName: 'input',
      props: {
        placeholder: '请输入企业名称',
      },
    },
    {
      label: '统一社会信用代码',
      prop: 'code',
      comName: 'input',
      props: {
        placeholder: '请输入统一社会信用代码',
      },
    },
    {
      label: '联系人',
      prop: 'contactUser',
      comName: 'input',
      props: {
        placeholder: '请输入联系人',
      },
    },
  ],
});
</script>